package com.example.util;

import com.example.models.Tower;

/**
 * A game map.
 * This class holds any "static" figure in the game according to its position.
 * every unit intersection would be calculated according to the game map.
 * @authors Itay Sorani, Nadav Ziv, Yonathan Zarsky
 */
public class GameMap {
	
	// a grid of map cells
	private MapCell[][] grid;
	
	/**
     * Constructs a new game map. initializing its cells.
     */
	public GameMap (int mapWidth, int mapHeight) {
		grid = new MapCell[mapWidth][mapHeight];
		for (int i = 0; i <mapWidth; i++) {
			for (int j = 0; j < mapHeight; j++) {
				grid[i][j] = new MapCell(null);
			}
		}
	}
	
	/**
     * returns a map cell according to the given position.
     */
	public MapCell getCell(int x, int y) {
		return grid[x][y];
	}
	
	/**
     * sets a map cell according to the given position.
     */
	public void setCell (int x,int y, MapCell cell) {
		this.grid[x][y] = cell;
	}
	
	/**
     * adds a figure to the given map cell.
     */
	public void addModel (int x, int y, Figure model) {
		if (canPlaceModel(x, y, model)) {
			
			this.grid[x][y].setModel(model);
		}
	}
	
	/**
     * removes a figure to the given map cell.
     */
	public void removeModel (Figure model) {
		int x = (int)model.getX();
		int y = (int)model.getY();

		this.grid[x][y].setModel(null);
	}
	
	/**
     * adds a path node to the given map cell.
     */
	public void setPathNode (PathNode pathNode) {
		this.grid[(int)pathNode.getX()][(int)pathNode.getY()].setPathNode(pathNode);
	}

	/**
     * removes a path node from the given map cell.
     */
	public void removePathNode (PathNode pathNode) {
		int x = (int)pathNode.getX();
		int y = (int)pathNode.getY();

		this.grid[x][y].setPathNode(null);
	}

	/**
     * adds a tower range node to the given map cell.
     */
	public void addTowerInRange (int x, int y, Tower tower) {
		this.grid[x][y].addTowerInRange(tower);
	}

	/**
     * removes a tower range node from the given map cell.
     */
	public void removeTowerInRange (Tower tower) {
		int x = (int)tower.getX();
		int y = (int)tower.getY();

		this.grid[x][y].removeTowerInRange(tower);
	}
	
	/**
     * checks if a figure can be placed in the given map cell or its already equipped.
     */
	public boolean canPlaceModel (int x, int y, Figure model) {
		if (this.grid[x][y].getModel() == null) {
			return (true);
		} else {
			return (false);
		}

	}
	
}
